package com.myjob.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.myjob.entity.*;
import com.myjob.enums.RedisEnums;
import com.myjob.mapper.*;
import com.myjob.response.Result;
import com.myjob.service.BaseInfoService;
import com.myjob.utils.RedisUtil;
import com.myjob.utils.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * @author chenyuan
 * @date 2021/1/17 11:52
 */
@Service
@Slf4j
public class BaseInfoServiceImpl implements BaseInfoService {
    @Autowired
    private BaseInfoMapper baseInfoMapper;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SkillMapper skillMapper;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 按照用户 Id 获取
     * @param userId 用户编号
     * @return
     */
    @Override
    public Result selectBaseInfoById(String userId) {
        Result result = null;
        if (UtilValidate.isEmpty(userId)){
            return Result.warn("用户 id 为空");
        }
        User user = userMapper.selectById(userId);
        BaseInfo baseInfo = baseInfoMapper.selectById(userId);
        if (UtilValidate.isEmpty(baseInfo) || UtilValidate.isEmpty(user)){
            return Result.failed("查询基本信息出错");
        }
        baseInfo.setName(user.getUserName())
                .setEmail(user.getEmail())
                .setPhone(user.getPhone());
        result = Result.succeed(baseInfo, "查询成功");
        return result;
    }

    /**
     * 修改 基本信息 baseInfo
     * @param baseInfo 基本信息
     * @return
     */
    @Override
    public Result updateBaseInfoById(BaseInfo baseInfo) {
        Result result = null;
        User user = new User();

        if (UtilValidate.isEmpty(baseInfo)){
            return Result.warn("基本信息为空");
        }
        // 修改 baseInfo
        String photo = (String) redisUtil.get(RedisEnums.KEY_IN_REDIS.USER_PHOTO.getValue() + baseInfo.getUserId() + ".png");
        log.info(photo);
        if (UtilValidate.isNotEmpty(photo)) {
            baseInfo.setPhoto(photo);
            user.setPhoto(photo);
        }
        int update = baseInfoMapper.updateById(baseInfo);

        // 修改 User
        int updateUser = 1;

        if (UtilValidate.isNotEmpty(baseInfo.getName())){
            user.setUserId(baseInfo.getUserId())
                    .setUserName(baseInfo.getName())
                    .setEmail(baseInfo.getEmail())
                    .setPhone(baseInfo.getPhone());
        }
        updateUser = userMapper.updateById(user);
        if (update <= 0 || updateUser <=0){
            return Result.failed("修改基本信息出错");
        }

        result = Result.succeed("修改成功");
        return result;
    }

    /**
     * 新增 基本信息 baseInfo
     * @param baseInfo
     * @return
     */
    @Override
    public Result insertBaseInfo(BaseInfo baseInfo) {
        Result result = null;
        if (UtilValidate.isEmpty(baseInfo)){
            return Result.warn("基本信息为空");
        }

        int insert = baseInfoMapper.insert(baseInfo);
        if (insert <= 0){
            return Result.failed("添加基本信息出错");
        }

        result = Result.succeed("添加成功");
        return result;
    }

    @Override
    public List<Skill> selectSkillById(String userId){
        QueryWrapper<Skill> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<Skill> skills = skillMapper.selectList(wrapper);
        return skills;
    }
}
